<template>
  <div v-loading.lock="loading" class="base-content">
    <div class="basic-info">
      <!--操作栏-->
      <div class="base-content-operate">
        <el-button
          v-rights="'Add'"
          icon="el-icon-plus"
          type="primary"
          @click="addRecord()"
          >生成</el-button
        >
        <el-button
          v-rights="'Export'"
          icon="el-icon-document-copy"
          type="primary"
          @click="exportData()"
          >导出</el-button
        >
        <el-button
          v-rights="'Delete'"
          icon="el-icon-delete"
          type="danger"
          @click="deleteRecord()"
          >删除</el-button
        >
      </div>
      <!--列表-->
      <el-table
        ref="table"
        class="filter unsetborder"
        :class="{ 'show-filter': showFilterRow }"
        :data="tableData"
        height="calc(100% - 190px)"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55">
          <el-table-column type="selection"></el-table-column>
        </el-table-column>
        <el-table-column align="center" width="50">
          <template #header>
            <div class="filter-icon" title="筛选">
              <SvgIcon
                class-name="filter"
                icon-class="filter"
                @click="filterIconClick"
              />
            </div>
          </template>
          <el-table-column width="50"></el-table-column>
        </el-table-column>

        <el-table-column label="记录编号">
          <el-table-column prop="num">
            <template #header>
              <el-input
                v-if="showFilterRow"
                v-model="filterForm.num"
                @keyup.enter.native="onQuery"
              ></el-input>
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column label="物料编号">
          <el-table-column prop="dataNum">
            <template #header>
              <el-input
                v-if="showFilterRow"
                v-model="filterForm.dataNum"
                @keyup.enter.native="onQuery"
              ></el-input>
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column label="物料名称">
          <el-table-column prop="dataName">
            <template #header>
              <el-input
                v-if="showFilterRow"
                v-model="filterForm.dataName"
                @keyup.enter.native="onQuery"
              ></el-input>
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column label="时间范围">
          <el-table-column prop="entrustOrderStatusName">
            <template slot-scope="scope">
              <span>{{
                formatTime(scope.row.selectStartTime) +
                "~" +
                formatTime(scope.row.selectEndTime)
              }}</span>
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column label="创建人">
          <el-table-column prop="creatorName">
            <template #header>
              <el-input
                v-if="showFilterRow"
                v-model="filterForm.creatorName"
                @keyup.enter.native="onQuery"
              ></el-input>
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column label="创建时间">
          <el-table-column prop="createTime">
            <template #header>
              <el-date-picker
                v-model="filterForm.createTimeList"
                type="datetimerange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd"
              >
              </el-date-picker>
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column label="操作">
          <el-table-column width="140">
            <template #header>
              <div v-if="showFilterRow" class="search-operate">
                <el-link type="primary" @click="onQuery">搜索</el-link>
                <el-link @click="cancelForm">重置</el-link>
              </div>
            </template>
            <template slot-scope="scope">
              <div class="base-table-operation">
                <el-link
                  v-rights="'Preview'"
                  type="primary"
                  @click="preview(scope.row)"
                  >预览</el-link
                >
              </div>
            </template>
          </el-table-column>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <BasePagination
        :total="pagination.total"
        :page-index="pagination.pageIndex"
        :page-size="pagination.pageSize"
        @sizeChange="sizeChange"
        @changePage="currentChange"
      />
    </div>
    <MaterialAirBottleUseRecordEdit
      v-if="dialogFormVisible"
      :dialog-form-visible.sync="dialogFormVisible"
      @close="closeEdit"
    />
    <BasePreviewFile
      v-if="previewFileShow"
      :preview-file-show.sync="previewFileShow"
      :preview-file="filepreview"
    />
  </div>
</template>
<script>
import {
  getList,
  deleteRecord,
} from "../../../api/Material/MaterialAirBottleUseRecord";
import MaterialAirBottleUseRecordEdit from "./MaterialAirBottleUseRecordEdit";
import { formatTime } from "../../../utils/time";
import { WordToImage, GetStaticFileInfo } from "@/api/Base";
export default {
  name: "MaterialAirBottleUseRecordList",
  components: { MaterialAirBottleUseRecordEdit },
  data() {
    return {
      formatTime,
      loading: false,
      showFilterRow: false,
      previewFileShow: false,
      dialogFormVisible: false,
      tableData: [],
      tableDatas: [],
      filepreview: {
        storageFileName: "",
        url: "",
      },
      filterForm: {
        num: "",
        dataNum: "",
        dataName: "",
        creatorName: "",
        createTimeList: [],
        createTimeStart: null,
        createTimeEnd: null,
      },
      pagination: {
        pageIndex: 1,
        pageSize: 10,
        total: 0,
      },
    };
  },
  computed: {},
  created() {
    this.getList();
  },
  methods: {
    preview(row) {
      this.loading = true;
      this.toImage(row.filePath);
    },

    toImage(data) {
      this.filepreview = {};
      this.filepreview.storageFileName = "";
      this.filepreview.url = "";
      this.filepreview.fileName = "气瓶使用记录.docx"; // 文件名称（包括后缀名）storageFileName
      this.filepreview.storageFileName = "气瓶使用记录.docx";
      this.filepreview.urlList = null;
      this.filepreview.type = "";

      WordToImage({ filePath: data })
        .then((res1) => {
          this.filepreview.urlList = res1.data;
          this.filepreview.type = "img";
          this.loading = false;
          this.previewFileShow = true;
        })
        .catch(() => {
          this.loading = false;
        });
    },
    closeEdit(data) {
      if (data) {
        this.loading = true;
        this.toImage(data);
      }
      this.onQuery();
    },
    onQuery() {
      if (
        this.filterForm.createTimeList !== null &&
        this.filterForm.createTimeList.length !== 0
      ) {
        this.filterForm.createTimeStart =
          this.filterForm.createTimeList[0] + " 00:00:00";
        this.filterForm.createTimeEnd =
          this.filterForm.createTimeList[1] + " 23:59:59";
      }

      this.pagination.pageIndex = 1;
      this.getList("filter");
    },
    getList(flag) {
      if (flag) {
        this.pagination.pageIndex = 1;
      }
      getList(this.filterForm, this.pagination).then((res) => {
        this.tableData = res.data.dataList;
        this.pagination.total = res.data.total;
      });
    },
    filterIconClick() {
      this.showFilterRow = !this.showFilterRow;
      !this.showFilterRow && this.cancelForm();
    },
    // 清空筛选条件
    cancelForm() {
      this.filterForm = this.$options.data().filterForm;
      this.pagination.pageIndex = 1;
      this.getList("filter");
    },
    // 新增
    addRecord() {
      this.dialogFormVisible = true;
    },
    // 导出
    exportData() {
      this.tableDatas.forEach((element) => {
        GetStaticFileInfo(element.filePath, element.num + "气瓶使用记录.docx");
      });
    },
    // 删除
    deleteRecord() {
      this.Helper.deleteConfirm(() => {
        this.loading = true;
        deleteRecord({ idList: this.tableDatas.map((x) => x.id) })
          .then((res) => {
            this.getList();
            this.loading = false;
          })
          .catch(() => {
            this.loading = false;
          });
      });
    },
    handleSelectionChange(val) {
      this.tableDatas = val;
    },
    // 切换页码
    sizeChange(val) {
      this.pagination.pageIndex = 1;
      this.pagination.pageSize = val;
      this.getList();
    },
    currentChange(val) {
      this.pagination.pageIndex = val;
      this.getList();
    },
  },
};
</script>
